<?php
namespace App\Utilitys;

class Upload
{
    //上传工具类

    /**
     * 上传图片到本地，返回地址
     * @return string
     */
    public static function uploadFile($request,$imgName='file')
    {
        $uploadSizeLimit = 10 * 1024 * 1023; //限制上传文件尺寸10M
        $suffix_img = [ //允许后缀
            "png", "jpg", "gif", "bmp", "jpeg", "jpe",
            "mp4", "mov", "rm", "rmvb", "avi", "mkv", "m4v",
            "docx", "doc", "xls", "xlsx", "pdf", "ppt", "pptx",
        ];
        if (!$request->hasFile($imgName)) {
            return array('code'=>-1,'msg'=>'没有文件上传');
        }
        //判断文件存在
        if ($_FILES[$imgName]['size'] > $uploadSizeLimit) {
            //echo "<script>alert('图片过大！');history.go(-1);</script>";exit;
            return array('code'=>-2,'msg'=>'文件尺寸过大');
        }
        $file = $request->file($imgName);
//        $data['fileSize'] = $file->getClientSize();
//        if ($data['fileSize'] > $uploadSizeLimit) {
//            //return false;
//            return array('code'=>-2,'msg'=>'文件尺寸过大');
//        }
        $fileExt = $file->getClientOriginalExtension();
        $fileExt = strtolower($fileExt);
        if ($fileExt && !in_array($fileExt, $suffix_img)) {
            return array('code'=>-3,'msg'=>'文件类型不合法');
        }
        if(!$file->isValid()){
            return array('code'=>-4,'msg'=>'文件错误');
        }
        $extension = $fileExt ?: 'png';
        $folderName = 'upload/'.date('Ymd', time()).'/';
        $safeName = time().uniqid().'.'.$extension;
        $file->move($folderName, $safeName);
        $filePath = $folderName.$safeName;//相对路径
        if (!$filePath) {
            return array('code'=>-5,'msg'=>'上传失败');
        }
        return array('code'=>0,'msg'=>'success','data'=>$filePath,'files'=>$_FILES);
    }

    /**
     * 上传图片到本地，返回地址：百度编辑器
     * @return string
     */
    public static function uploadBdFile($request,$imgName='file')
    {
        $uploadSizeLimit = 10 * 1024 * 1023; //限制上传文件尺寸10M
        $suffix_img = [ //允许后缀
            "png", "jpg", "gif", "bmp", "jpeg", "jpe", "xls", "xlsx",
            "mp4", "mov", "rm", "rmvb", "avi", "mkv", "m4v",
        ];
        if (!$request->hasFile($imgName)) {
            return array('code'=>-1,'msg'=>'没有文件上传');
        }
        //判断文件存在
        if ($_FILES[$imgName]['size'] > $uploadSizeLimit) {
            return array('code'=>-2,'msg'=>'文件尺寸过大');
        }
        $file = $request->file($imgName);
        $fileExt = $file->getClientOriginalExtension();
        $fileExt = strtolower($fileExt);
        if ($fileExt && !in_array($fileExt, $suffix_img)) {
            return array('code'=>-3,'msg'=>'文件类型不合法');
        }
        if(!$file->isValid()){
            return array('code'=>-4,'msg'=>'文件错误');
        }
        $extension = $fileExt ?: 'png';
        $folderName = 'uploadBd/'.date('Ymd', time()).'/';
        $safeName = time().uniqid().'.'.$extension;
        $file->move($folderName, $safeName);
        $filePath = $folderName.$safeName;//相对路径
        if (!$filePath) {
            return array('code'=>-5,'msg'=>'上传失败');
        }
        return array('code'=>0,'msg'=>'success','data'=>$filePath,'files'=>$_FILES);
    }

    /**
     * 文件转base64输出
     * @param $filePath 文件路径
     */
    public static function getBase64ByPath($filePath)
    {
        if(!file_exists($filePath)){ return ''; }
        $mimeType = mime_content_type($filePath);
        $base64Data = base64_encode(file_get_contents($filePath));
        return 'data:'.$mimeType.';base64,'.$base64Data;
    }

    /**
     * 将Base64图片转换为本地图片并保存
     * @param  $base64 base64图片码
     */
    public static function getPathByBase64($base64)
    {
        $prefix = 'upload/'.date('Ymd', time()).'/';
        if(!file_exists($prefix)){ mkdir($prefix, 0777); }
        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $result)){
            $type = $result[2];
            $prefix = $prefix . time() . uniqid() . '.' . $type;
            $resPut = file_put_contents($prefix, base64_decode(str_replace($result[1], '', $base64)));
            return $resPut ? $prefix : '';
        } else {
            $prefix = $prefix . time() . uniqid() . '.png';
            $resPut = file_put_contents($prefix, base64_decode($base64));
            return $resPut ? $prefix : '';
        }
    }
}